layui.use(['layer', 'form', 'table', 'admin', 'ax', 'func','index'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var index = layui.index;

    /**
     * 流程管理--流程定义
     */
    var Definition = {
        tableId: "definitionTable",    //表格id
        condition: {
            modelName: "",
            key:""
        }
    };
    /**
     * 初始化表格的列
     */
    Definition.initColumn = function () {
        return [[
            {field: 'id', type:"numbers",title: "编号"},
            {field: 'key',  sort: true, title: 'KEY',align: "center" },
            {field: 'name', align: "center", title: '流程名称',width: 100 ,templet:function (d) {
                    return "-";
                }},
            {field: 'version', align: "center",  width:100,title: 'version'},
            {field: 'description', align: "center", title: '流程描述'},
            {field: 'deploymentTime', align: "center", sort: true, title: '部署时间'},
            {field: 'resourceName', align: "center", title: '流程定义',templet:function (d){
                    return `<a style="color: #337ab7" download="" href="#" >d.resourceName</a>`;
                }},
            {field: 'resourceName', align: "center", title: '流程图',templet:function (d){
                    return `<a style="color: #337ab7" download="" href="#" >d.diagramResourceName</a>`;
                }},

            {field: 'suspendStateName', align: "center", sort: true, title: '流程定义状态'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 250}
        ]];
    };


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Definition.tableId,
        url: Feng.ctxPath + '/definition/list',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: Definition.initColumn()
    });

    /**
     * 点击查询按钮
     */
    Definition.search = function () {
        var queryData = {};
        queryData['name'] = $("#name").val();
        queryData['key'] = $("#KEY").val();
        queryData['category'] = $("#category").val();
        table.reload(Definition.tableId, {
            where: queryData, page: {curr: 1}
        });
    };
    
    $("#btnAdd").click(function () {
        Definition.openAddModel();
    })


    /**
     * 激活或挂起流程
     * @param data
     */
    Definition.onSuspendOrActiveApply = function (data){
        var msg = data.suspendState == 1 ? "挂起" : "激活";
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/definition/suspendOrActiveApply", function (res) {
                Feng.success(msg + "成功！");
                table.reload(Definition.tableId);
            }, function (data) {
                Feng.error("添加失败！" + data.responseJSON.message)
            });
            ajax.set({id: data.id, suspendState: data.suspendState});
            ajax.start();
        }
        Feng.confirm("是否"+ msg + "流程?", operation);
    }

    $("#btnSearch").click(function () {
        Definition.search();
    })

    Definition.onConvertModel = function (data){
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/definition/convert2Model", function () {
                Feng.success("转换成功!");
                table.reload(Definition.tableId);
            }, function (data) {
                Feng.error("转换失败!" + data.responseJSON.message + "!");
            });
            ajax.set("processDefinitionId", data.id);
            ajax.start();
        };
        Feng.confirm("是否转换为流程模型?", operation);
    }

    Definition.delete = function(data){
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/definition/removeDefinition", function () {
                Feng.success("删除成功!");
                table.reload(Definition.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("ids", data.deploymentId);
            ajax.start();
        };
        Feng.confirm("是否删除流程模型 " + data.name + "?", operation);
    }



    Definition.deployDefinition = function (data){
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/definition/deployDefinition/"+data.id, function () {
                Feng.success("部署成功!");
                table.reload(Definition.tableId);
            }, function (data) {
                Feng.error("部署失败!" + data.responseJSON.message + "!");
            });
            ajax.start();
        };
        Feng.confirm("是否部署流程模型 " + data.name + "?", operation);
    }
    // 工具条点击事件
    table.on('tool(' + Definition.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        console.log(data)
        if (layEvent === 'suspendOrActive') {
            Definition.onSuspendOrActiveApply(data);
        }else if (layEvent === 'convertModel'){
            Definition.onConvertModel(data);
        }else{
            Definition.delete(data);
        }
    });

});
